﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICUP13.Management;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using ICUP13.Texture;

namespace ICUP13.States
{
    public class GamePlayState : IBaseState
    {
        TextureManager tm = TextureManager.GetInstance();
        // Constructor
        public GamePlayState() { }

        // State Methods
        public override void Enter()
        {
        }
        public override void Exit()
        {
        }
        KeyboardState LastKeys = Keyboard.GetState();
        public override bool Input(float Delta, float TotalElapsed, int FPS)
        {
            KeyboardState CurrentKeys = Keyboard.GetState();

            if (CurrentKeys.IsKeyDown(Keys.Escape) && LastKeys.IsKeyUp(Keys.Escape))
                return false;
            else if (CurrentKeys.IsKeyDown(Keys.P) && LastKeys.IsKeyUp(Keys.P))
                StateMachine.AddStateQueue(new PauseScreenState());
            else if ((CurrentKeys.IsKeyDown(Keys.M) && LastKeys.IsKeyUp(Keys.M)))
                StateMachine.BackStateQueue();
            LastKeys = CurrentKeys;

            return true;
        }
        public override void Update(float Delta, float TotalElapsed, int FPS)
        {

        }
        public override void Draw(float Delta, float TotalElapsed, int FPS)
        {
            clearScreen(TotalElapsed);
            Rectangle desRect = MainGame.ClientRect;
            double sinWave = Math.Sin(TotalElapsed*0.25f);
            double cosWave = Math.Cos(TotalElapsed*0.25f);
            int xScale = (int)(Math.Abs(sinWave) * MainGame.ClientRect.Width * 0.5f) - (int)(MainGame.ClientRect.Width * 0.5f);
            int yScale = (int)(Math.Abs(cosWave) * 30);

            desRect.Inflate(xScale, yScale);
            MainGame.Batch.Draw(tm.GamePlayScreenBackground, desRect, null, Color.White, 0, Vector2.Zero, sinWave < 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
        }
        
        private void clearScreen(float TotalElapsed)
        {
            float rValue = (float)(1.0f - Math.Sin(TotalElapsed));
            float gValue = (float)(Math.Sin(TotalElapsed));
            float bValue = (float)(Math.Cos(TotalElapsed));
            MainGame.Device.Clear(new Color(rValue, gValue, bValue, 1f));
        }
    }
}
